/**
 * @description: 下载文件
 * @param stream 文件源，string or buffer
 * @param name 文件名称
 * @param mimeType type josn
 * @return new tree data  纯函数
 */
export const downFile = (stream: string | ArrayBuffer, name: string, mimeType: string = "text/plain;charset=utf-8") => {
  // 创建 Blob 对象
  let blob: Blob;
  if (typeof stream === "string") {
    blob = new Blob([stream], { type: mimeType });
  } else {
    blob = new Blob([stream], { type: mimeType });
  }

  // 创建下载链接并触发下载
  const a = document.createElement("a");
  a.href = URL.createObjectURL(blob);
  a.download = name;
  a.style.display = "none";
  document.body.appendChild(a);
  a.click();
  document.body.removeChild(a);

  // 清理创建的 URL 对象
  URL.revokeObjectURL(a.href);
};
